/**
 * Created by CAOTE on 11/6/2017.
 */
require('../model/serviceInfoModel.js');
var mongoose = require('mongoose');
var serviceInfoModel = mongoose.model('ServiceInfo');
var dateUtil = require('../util/dateUtil');
var _ = require('lodash');
exports.updateServiceInfo = updateServiceInfo;
exports.getServiceInfo = getServiceInfo;
function updateServiceInfo(data, callback){
    if(_.isEmpty(data)){
        return;
    }
    if(data.type === 'Update'){
        editServiceInfo(data.serviceInfo,callback)

    }
    else if(data.type === 'New'){
        createNewServiceInfo(data.serviceInfo,callback);
    }
    else if(data.type === 'Delete'){
        deleteServiceInfo(data.serviceInfo,callback)

    }
}
function createNewServiceInfo(data, callback) {
    data.updateDate = dateUtil.getNowFormatDate();
    serviceInfoModel.create(data, function(err, result){
        if(err){
            callback(err, null);
        }else{
            callback(null, result)
        }
    })
}

function getServiceInfo(criteria, callback){
    serviceInfoModel.find(criteria, function(err, result){
        if(err){
            console.log(err);
        }else{
            callback(err, result);
        }
    })
}
function editServiceInfo(newRecord, callback){
    newRecord.updateDate = dateUtil.getNowFormatDate();
    serviceInfoModel.update({_id:newRecord._id},{$set:newRecord}, function(err,result){
        if(err){
            callback(err, null);
        }else{
            callback(null, result)
        }
    });
}


function deleteServiceInfo(deleteRecord, callback){
    serviceInfoModel.remove({_id:deleteRecord._id}, function(err,result){
        if(err){
            callback(err, null);
        }else{
            callback(null, result)
        }
    });
}